package com.liuxu.topnews.Adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.liuxu.topnews.R;
import com.liuxu.topnews.bean.App;

import java.util.ArrayList;

public class RecycleAdapter extends RecyclerView.Adapter<RecycleAdapter.MyHolder> {
    Context context;
    public ArrayList<App> apps;
    public RecycleAdapter(Context context,ArrayList<App> apps){
        this.context=context;
        this.apps=apps;
    }

    public void update(ArrayList<App> apps){
        this.apps=apps;
        notifyDataSetChanged();
    }
    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public MyHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view= LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.recycler_view,viewGroup,false);
        MyHolder myHolder=new MyHolder(view);
        return myHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull MyHolder myHolder, final int i) {
        myHolder.textView1.setText(apps.get(i).getId());
        myHolder.textView2.setText(apps.get(i).getZname());

        if (myHolder.editText1.getTag() instanceof TextWatcher){
            myHolder.editText1.removeTextChangedListener ((TextWatcher) myHolder.editText1.getTag());
        }
        if (myHolder.editText2.getTag() instanceof TextWatcher){
            myHolder.editText2.removeTextChangedListener ((TextWatcher) myHolder.editText2.getTag());
        }
        if (myHolder.editText3.getTag() instanceof TextWatcher){
            myHolder.editText3.removeTextChangedListener ((TextWatcher) myHolder.editText3.getTag());
        }
        myHolder.editText1.setText(String.valueOf(apps.get(i).getUptime()));
        myHolder.editText2.setText(String.valueOf(apps.get(i).getDowntime()));
        myHolder.editText3.setText(String.valueOf(apps.get(i).getPagenumber()));

        myHolder.textView1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        TextWatcher editText1watcher=new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (!s.toString().equals("")){
                    apps.get(i).setUptime(Integer.parseInt(s.toString()));
                }
            }
        };
        TextWatcher editText2watcher=new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (!s.toString().equals("")){
                    apps.get(i).setDowntime(Integer.parseInt(s.toString()));
                }
            }
        };
        TextWatcher editText3watcher=new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (!s.toString().equals("")){
                    apps.get(i).setPagenumber(Integer.parseInt(s.toString()));
                }
            }
        };
        myHolder.editText1.addTextChangedListener(editText1watcher);
        myHolder.editText2.addTextChangedListener(editText2watcher);
        myHolder.editText3.addTextChangedListener(editText3watcher);
        myHolder.editText1.setTag(editText1watcher);
        myHolder.editText2.setTag(editText2watcher);
        myHolder.editText3.setTag(editText3watcher);
    }

    @Override
    public int getItemCount() {
        return apps.size();
    }

    class MyHolder extends RecyclerView.ViewHolder{
        TextView textView1;
        TextView textView2;
        EditText editText1;
        EditText editText2;
        EditText editText3;
        public MyHolder(@NonNull View itemView) {
            super(itemView);
            textView1=itemView.findViewById(R.id.appid);
            textView2=itemView.findViewById(R.id.appname);
            editText1=itemView.findViewById(R.id.word1);
            editText2=itemView.findViewById(R.id.word2);
            editText3=itemView.findViewById(R.id.word3);
        }
    }
}
